home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / defrasterPort / defo.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  301 lines

  1. /*
  2.  * Copyright (c) 1994, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*
  23.  * Example to convert (and render) a font defined by defrasterfont()
  24.  * in irisGL to an openGL bitmap
  25.  *
  26.  * Yusuf Attarwala
  27.  *
  28.  * cc defo.c -o defo -lGL -lGLw -lX11 -lXt -lXm
  29.  *
  30.  */
  31.  
  32. #include <X11/Xlib.h>
  33. #include <Xm/Form.h>
  34. #include <Xm/Frame.h>
  35.  
  36. #include <GL/gl.h>
  37. #include <GL/glu.h>
  38. #include <GL/GLwMDrawA.h> 
  39.  
  40. int test_width  = 33;
  41. int test_height = 33;
  42.  
  43. unsigned short      test_raster[] = {
  44.         0x000f, 0xf000, 0x0000,
  45.         0x0010, 0x0800, 0x0000,
  46.         0x0060, 0x0600, 0x0000,
  47.         0x0180, 0x0180, 0x0000,
  48.         0x0600, 0x0060, 0x0000,
  49.         0x0800, 0x0010, 0x0000,
  50.         0x0803, 0xc010, 0x0000,
  51.         0x100f, 0xf008, 0x0000,
  52.         0x1038, 0x1c08, 0x0000,
  53.         0x2060, 0x0604, 0x0000,
  54.         0x2080, 0x0104, 0x0000,
  55.         0x4000, 0x0002, 0x0000,
  56.         0x8000, 0x0001, 0x0000,
  57.         0x8003, 0xc001, 0x0000,
  58.         0x8003, 0xc001, 0x0000,
  59.         0x8001, 0x8001, 0x0000,
  60.         0x8000, 0x0001, 0x0000,
  61.         0x8000, 0x0001, 0x0000,
  62.         0x8070, 0x0e01, 0x0000,
  63.         0x8070, 0x0e01, 0x0000,
  64.         0x8070, 0x0e01, 0x0000,
  65.         0x4070, 0x0e02, 0x0000,
  66.         0x2000, 0x0004, 0x0000,
  67.         0x2000, 0x0004, 0x0000,
  68.         0x1000, 0x0008, 0x0000,
  69.         0x1000, 0x0008, 0x0000,
  70.         0x0800, 0x0010, 0x0000,
  71.         0x0800, 0x0010, 0x0000,
  72.         0x0600, 0x0060, 0x0000,
  73.         0x01ff, 0xff80, 0x0000,
  74.         0x007f, 0xfe00, 0x0000,
  75.         0x001f, 0xf800, 0x0000,
  76.         0x000f, 0xf000, 0x0000};
  77.  
  78. GLubyte *defToO();
  79.  
  80. #define BITUNIT 8
  81.  
  82. int width,height;
  83. Display *display; 
  84. XtAppContext  appContext; 
  85. XVisualInfo *vi;
  86.  
  87. Widget     toplevel,glw;
  88. GLXContext glxc;
  89.  
  90. GLubyte *stip;
  91.  
  92. Arg args[20];
  93. int acnt;
  94.  
  95. static void createToplevel(),
  96.             drawScene(),
  97.             setMatrix(),
  98.             exposeCB(),
  99.             resizeCB(),
  100.             initCB();
  101.  
  102. void
  103. testBitmap(raster)
  104. GLubyte *raster;
  105. {
  106.  
  107.     stip = raster;
  108.     createToplevel();             /* create and realize widget hierarchy */
  109.     XtAppMainLoop(appContext);    /* loop for ever */
  110.  
  111. }
  112.  
  113. void
  114. createToplevel(void)
  115. {
  116.     Widget frame;
  117.  
  118.     acnt = 0;
  119.     XtSetArg(args[acnt],XmNminHeight, 200);acnt++;
  120.     XtSetArg(args[acnt],XmNminWidth,  200);acnt++;
  121.     toplevel  = XtAppCreateShell("openGL bitmap","obit",
  122.                                   applicationShellWidgetClass,
  123.                                   display,args,acnt);
  124.  
  125.     /* create a frame to hold the glx drawing area */
  126.     frame   = XtVaCreateManagedWidget("frame",
  127.                               xmFrameWidgetClass, toplevel,
  128.                               NULL);
  129.  
  130.     /* create and manage a single buffer widget, rgb mode */
  131.     acnt = 0;
  132.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  133.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  134.     XtManageChild(glw);
  135.  
  136.     /* register callbacks */
  137.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  138.  
  139.     /* realize widget hierarchy */
  140.     XtRealizeWidget(toplevel);
  141.  
  142.     /* additional callbacks */
  143.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  144.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  145.  
  146. }
  147.  
  148. void
  149. drawScene()
  150. {
  151.     glClearColor(0.8, 0.8, 0.8, 0.0);
  152.     glClear(GL_COLOR_BUFFER_BIT);
  153.     glColor3f(0.0,0.0,0.0);
  154.     glRasterPos2f(20.5,20.5);
  155.     glBitmap(width,height,0.0,0.0,width+2,0.0,stip);
  156.     glFlush();
  157.  
  158. }
  159.  
  160. static void
  161. setMatrix()
  162. {
  163.     glMatrixMode(GL_PROJECTION);
  164.     glLoadIdentity();
  165.     glOrtho(0.0,100.0,0.0,100.0,-1.0,1.0); /* a simple ortho */
  166.     glMatrixMode(GL_MODELVIEW);
  167.     glLoadIdentity();                      /* default view */
  168. }
  169.  
  170. static void
  171. resizeCB(w,client_data,call)
  172. Widget w;
  173. XtPointer client_data;
  174. XtPointer call;
  175. {
  176.     GLwDrawingAreaCallbackStruct *call_data;
  177.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  178.  
  179.     GLwDrawingAreaMakeCurrent(w, glxc);
  180.     glViewport(0, 0, call_data->width, call_data->height);
  181.     setMatrix();
  182.     drawScene();
  183. }
  184.  
  185. static void
  186. exposeCB(w,client_data,call)
  187. Widget w;
  188. XtPointer client_data;
  189. XtPointer call;
  190. {
  191.     GLwDrawingAreaCallbackStruct *call_data;
  192.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  193.  
  194.     GLwDrawingAreaMakeCurrent(w, glxc);
  195.     drawScene();
  196. }
  197.  
  198. static void
  199. initCB(w,client_data,call)
  200. Widget w;
  201. XtPointer client_data;
  202. XtPointer call;
  203. {
  204.     XVisualInfo *vi;
  205.  
  206.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  207.     XtGetValues(w, args, 1);
  208.  
  209.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  210.  
  211.     GLwDrawingAreaMakeCurrent(w, glxc);
  212.     glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  213. }
  214.  
  215.  
  216. void
  217. main(argc,argv)
  218. int argc;
  219. char **argv;
  220. {
  221.     GLubyte *raster;
  222.  
  223.     XtToolkitInitialize();
  224.     appContext = XtCreateApplicationContext();
  225.     display    = XtOpenDisplay(appContext, NULL, "DefToO","defToO",NULL,0,
  226.                               &argc,argv);
  227.  
  228.     raster = defToO(test_width,test_height,test_raster);
  229.  
  230.     testBitmap(raster);
  231.  
  232. }
  233.  
  234. /* defraster bitmap to openGL bitmap */
  235.  
  236. GLubyte *
  237. defToO(w,h,r)
  238. int w,h;
  239. unsigned short *r;
  240. {
  241.     GLubyte *xbm,*uc;
  242.     unsigned short *us;
  243.  
  244.     int i,j;
  245.     int wide;
  246.     int tokensPerRow;
  247.     int outTokensPerRow;
  248.  
  249.     width   = w;
  250.     height  = h;
  251.  
  252.     if (width <= 16) {
  253.         tokensPerRow = 1;
  254.     }
  255.     else {
  256.     if (width%16 == 0) {
  257.         tokensPerRow = (int)(width / 16);
  258.     }
  259.     else {
  260.         tokensPerRow = (int)(width / 16) + 1;
  261.     }
  262.     }
  263.  
  264.     if (width <= BITUNIT) {
  265.     outTokensPerRow = 1;
  266.     }
  267.     else {
  268.     if (width%BITUNIT == 0) {
  269.         outTokensPerRow = (int)(width / BITUNIT);
  270.     }
  271.     else {
  272.         outTokensPerRow = (int)(width / BITUNIT) + 1;
  273.     }
  274.     }
  275.  
  276.     xbm = (GLubyte *)malloc(BITUNIT * outTokensPerRow * height);
  277.  
  278.     us = r;
  279.     for (i=0;i<height;i++) {            /* bottom top */
  280.         uc = xbm + i*outTokensPerRow;
  281.         wide = 0;
  282.         for (j=0;j<tokensPerRow;j++) {
  283.  
  284.         *uc  = (GLubyte)(*us >> 8);
  285.  
  286.         uc++;
  287.         wide += 8;
  288.  
  289.         if (wide < width) {
  290.             *uc  = (GLubyte)(*us);
  291.             uc++;
  292.             wide += 8;
  293.         }
  294.  
  295.         us++;
  296.         }
  297.     }
  298.  
  299.     return(xbm);
  300. }
  301.